package com.zjh.springcloud.controller;

import com.zjh.springcloud.feign.AccountFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

/**
 * ClassName: AccountClientController
 * Description:
 * @Author zjh
 * @Create 2023/10/30 11:28
 */
@Controller
@RequestMapping("accountClient")
public class AccountClientController {
	
	@Autowired
	private AccountFeign accountFeign;
	
	@GetMapping("toLogin")
	public String toLogin(){
		return "login";
	}
	
	@PostMapping("/login")
	public String login(@RequestParam("username")String username,
	                    @RequestParam("password")String password,
	                    @RequestParam("type") String type, ModelMap map, HttpSession session){
		Object obj =accountFeign.login(username,password,type);
		System.out.println(obj);
		if(obj!=null){
			
			session.setAttribute("obj",obj);
			if("user".equals(type))
				return "index";
			return "main";
		}
		else{
			map.addAttribute("login_error","账号或密码错误");
			return "login";
		}
	}
	
	@GetMapping("logout")
	public String logout(HttpSession session){
		session.removeAttribute("obj");
		session.invalidate();
		return "redirect:/accountClient/toLogin";
	}
}
